package com.fun.today.https;

import com.fun.today.utils.LogUtils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/11/30 0030.
 */

public class HttpUtils
{
	
	public HttpUtils()
	{
	}
	
	private static class HttpUtilsHolder
	{
		public static HttpUtils instance = new HttpUtils();
	}
	
	public static String requestGet(
			String appKey,
			int month,
			int day,
			String baseUrl )
	{
		String result = null;
		try
		{
			String requestUrl = baseUrl + "key=" + appKey + "&month=" + month + "&day=" + day;
			LogUtils.i( " requestGet() requestUrl = " + requestUrl );
			URL url = new URL( requestUrl );
			HttpURLConnection urlConn = ( HttpURLConnection )url.openConnection();
			urlConn.setConnectTimeout( 5 * 1000 );
			urlConn.setReadTimeout( 5 * 1000 );
			urlConn.setUseCaches( true );
			urlConn.setRequestMethod( "GET" );
			urlConn.setRequestProperty( "Content-Type", "application/json" );
			urlConn.addRequestProperty( "Connection", "Keep-Alive" );
			urlConn.connect();
			if( urlConn.getResponseCode() == 200 )
			{
				result = streamToString( urlConn.getInputStream() );
				LogUtils.i( "Get方式请求成功，" );
			}
			else
			{
				LogUtils.i( "Get方式请求失败" );
			}
			// 关闭连接
			urlConn.disconnect();
		}
		catch( Exception e )
		{
			LogUtils.e( e.toString() );
		}
		return result;
	}
	
	private void requestPost(
			HashMap< String, String > paramsMap,
			String baseUrl )
	{
		try
		{
			//            String baseUrl = "https://xxx.com/getUsers";
			//合成参数
			StringBuilder tempParams = new StringBuilder();
			int pos = 0;
			for( String key : paramsMap.keySet() )
			{
				if( pos > 0 )
				{
					tempParams.append( "&" );
				}
				tempParams.append( String.format( "%s=%s", key, URLEncoder.encode( paramsMap.get( key ), "utf-8" ) ) );
				pos++;
			}
			String params = tempParams.toString();
			// 请求的参数转换为byte数组
			byte[] postData = params.getBytes();
			// 新建一个URL对象
			URL url = new URL( baseUrl );
			// 打开一个HttpURLConnection连接
			HttpURLConnection urlConn = ( HttpURLConnection )url.openConnection();
			// 设置连接超时时间
			urlConn.setConnectTimeout( 5 * 1000 );
			//设置从主机读取数据超时
			urlConn.setReadTimeout( 5 * 1000 );
			// Post请求必须设置允许输出 默认false
			urlConn.setDoOutput( true );
			//设置请求允许输入 默认是true
			urlConn.setDoInput( true );
			// Post请求不能使用缓存
			urlConn.setUseCaches( false );
			// 设置为Post请求
			urlConn.setRequestMethod( "POST" );
			//设置本次连接是否自动处理重定向
			urlConn.setInstanceFollowRedirects( true );
			// 配置请求Content-Type
			urlConn.setRequestProperty( "Content-Type", "application/json" );
			// 开始连接
			urlConn.connect();
			// 发送请求参数
			DataOutputStream dos = new DataOutputStream( urlConn.getOutputStream() );
			dos.write( postData );
			dos.flush();
			dos.close();
			// 判断请求是否成功
			if( urlConn.getResponseCode() == 200 )
			{
				// 获取返回的数据
				String result = streamToString( urlConn.getInputStream() );
				LogUtils.i( "Post方式请求成功，result--->" + result );
			}
			else
			{
				LogUtils.i( "Post方式请求失败" );
			}
			// 关闭连接
			urlConn.disconnect();
		}
		catch( Exception e )
		{
			LogUtils.e( e.toString() );
		}
	}
	
	/**
	 * 将输入流转换成字符串
	 * @param is 从网络获取的输入流
	 */
	private static String streamToString( InputStream is )
	{
		try
		{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[ 1024 ];
			int len = 0;
			while( ( len = is.read( buffer ) ) != -1 )
			{
				baos.write( buffer, 0, len );
			}
			baos.close();
			is.close();
			byte[] byteArray = baos.toByteArray();
			return new String( byteArray );
		}
		catch( Exception e )
		{
			LogUtils.e( e.toString() );
			return null;
		}
	}
}
